package com.gopay.bis.dao.bankorder.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.gopay.bis.dao.bankorder.BankContractSerialInfoDAO;
import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import com.gopay.common.constants.bank.BankContractConstants;
import com.gopay.common.domain.bis.BisBankContractSerialInfo;
import com.gopay.common.util.DateUtils;

/**
 * 签约流水表接口实现
 * 
 * @BankContractSerialInfoDAOImpl.java
 * @author zhengxue
 * @2016-12-14 下午3:03:08 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository(value = "bankContractSerialInfoDAO")
public class BankContractSerialInfoDAOImpl extends BaseDAOImpl<BisBankContractSerialInfo, String> implements
        BankContractSerialInfoDAO {

    @Override
    public void insertContractSerialInfo(BisBankContractSerialInfo contractSerialInfo) {
        Date curDate = DateUtils.getSystemDate();
        contractSerialInfo.setCreatedTs(curDate);
        contractSerialInfo.setLastUpdTs(curDate);
        this.getHibernateTemplate().save(contractSerialInfo);

    }

    @Override
    public int updateContractStateByCardNo(BisBankContractSerialInfo contractSerialInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("update BisBankContractSerialInfo set ");
        hql.append("contractState = :contractState, ");
        hql.append("lastUpdTs = :lastUpdTs ");
        hql.append("where bankCode =:bankCode ");
        hql.append("and bankBizProdCode =:bankBizProdCode ");
        hql.append("and intrfaceChannelCode =:intrfaceChannelCode ");
        // edit hougn 2017-04-17 央行检测敏感信息整改  start
        hql.append("and cardNo in (:cardNo) ");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("contractState", contractSerialInfo.getContractState());
        params.put("lastUpdTs", DateUtils.getSystemDate());
        params.put("bankCode", contractSerialInfo.getBankCode());
        params.put("bankBizProdCode", contractSerialInfo.getBankBizProdCode());
        params.put("intrfaceChannelCode", contractSerialInfo.getIntrfaceChannelCode());
        List<String> cardNoList = SensitiveInfoUtils.getAllHisCipherText(contractSerialInfo.getCardNo());
        params.put("cardNo", cardNoList);
        // edit hougn 2017-04-17 央行检测敏感信息整改  end

        return this.updateByHql(hql.toString(), params);
    }

    @Override
    public int updateContractStateBySerialNum(BisBankContractSerialInfo contractSerialInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("update BisBankContractSerialInfo set ");
        hql.append("contractState = :contractState, ");
        hql.append("lastUpdTs = :lastUpdTs ");
        hql.append("where serialNumber =:serialNumber ");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("contractState", contractSerialInfo.getContractState());
        params.put("lastUpdTs", DateUtils.getSystemDate());
        params.put("serialNumber", contractSerialInfo.getSerialNumber());
        return this.updateByHql(hql.toString(), params);
    }

    @Override
    public BisBankContractSerialInfo getContractInfobySerialNum(BisBankContractSerialInfo contractSerialInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("from BisBankContractSerialInfo where bankCode=:bankCode ");
        hql.append("and bankBizProdCode=:bankBizProdCode ");
        hql.append("and intrfaceChannelCode=:intrfaceChannelCode ");
        hql.append("and serialNumber=:serialNumber ");

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("bankCode", contractSerialInfo.getBankCode());
        param.put("bankBizProdCode", contractSerialInfo.getBankBizProdCode());
        param.put("intrfaceChannelCode", contractSerialInfo.getIntrfaceChannelCode());
        param.put("serialNumber", contractSerialInfo.getSerialNumber());

        List<BisBankContractSerialInfo> infoQuerys = this.findByHql(hql.toString(), param);

        if (infoQuerys != null && infoQuerys.size() != 0) {
            return infoQuerys.get(0);
        } else {
            return null;
        }
    }

    @Override
    public BisBankContractSerialInfo getContractInfobyCard(BisBankContractSerialInfo contractSerialInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("from BisBankContractSerialInfo where bankCode=:bankCode ");
//        hql.append("and bankBizProdCode=:bankBizProdCode ");
        hql.append("and intrfaceChannelCode=:intrfaceChannelCode ");
        // edit hougn 2017-04-17 央行检测敏感信息整改  start
        hql.append("and cardNo in (:cardNo) ");
        if (StringUtils.isNotBlank(contractSerialInfo.getContractDesc())) {
            hql.append("and contractDesc=:contractDesc ");
        }
        hql.append("order by createdTs desc");

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("bankCode", contractSerialInfo.getBankCode());
//        param.put("bankBizProdCode", contractSerialInfo.getBankBizProdCode());
        param.put("intrfaceChannelCode", contractSerialInfo.getIntrfaceChannelCode());
        List<String> cardNoList = SensitiveInfoUtils.getAllHisCipherText(contractSerialInfo.getCardNo());
        param.put("cardNo", cardNoList);
        // edit hougn 2017-04-17 央行检测敏感信息整改  end
        if (StringUtils.isNotBlank(contractSerialInfo.getContractDesc())) {
            param.put("contractDesc", contractSerialInfo.getContractDesc());
        }
        List<BisBankContractSerialInfo> infoQuerys = this.findByHql(hql.toString(), param);

        if (infoQuerys != null && infoQuerys.size() != 0) {
            for (int i = 0; i < infoQuerys.size(); i++) {
                if (BankContractConstants.CONTRACT_SUCCESS.equals(infoQuerys.get(i).getContractState())) {
                    return infoQuerys.get(i);
                }
            }
            return null;
        } else {
            return null;
        }
    }

    
    @Override
    public BisBankContractSerialInfo getContractInfobyCardAndEleValue(BisBankContractSerialInfo contractSerialInfo) {
        StringBuffer hql = new StringBuffer();
        hql.append("from BisBankContractSerialInfo where bankCode=:bankCode ");
        hql.append("and bankBizProdCode=:bankBizProdCode ");
        hql.append("and intrfaceChannelCode=:intrfaceChannelCode ");
        //hql.append("and cardNo=:cardNo ");
        hql.append("and contractDesc=:contractDesc ");
        hql.append("and elementsValue=:elementsValue ");
        hql.append("order by createdTs desc");

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("bankCode", contractSerialInfo.getBankCode());
        param.put("bankBizProdCode", contractSerialInfo.getBankBizProdCode());
        param.put("intrfaceChannelCode", contractSerialInfo.getIntrfaceChannelCode());
        //param.put("cardNo", contractSerialInfo.getCardNo());
        param.put("contractDesc", contractSerialInfo.getContractDesc());
        param.put("elementsValue", contractSerialInfo.getElementsValue());
        List<BisBankContractSerialInfo> infoQuerys = this.findByHql(hql.toString(), param);
        if(infoQuerys == null || infoQuerys.size() == 0){
        	return null;
        }
        return infoQuerys.get(0);
    }
}
